# settings.py

import os
from pathlib import Path

# 项目根目录
BASE_DIR = Path(__file__).resolve().parent.parent

# 安全设置
SECRET_KEY = "django-insecure-*#pve7a-j#z-4cx^=x-r_v#5*)jm*=&n)dr4k@#w)=g8#d^9b3"
BAIDU_APPID = os.getenv('BAIDU_APPID', '20241224002237053')
BAIDU_APPKEY = os.getenv('BAIDU_APPKEY', 'tW49ZyLvTBsJLMSIGbX5')
# DEBUG 设置
DEBUG = True

# 允许的主机
ALLOWED_HOSTS = ['*']  # 在开发阶段，可以临时设置为允许所有主机

# CORS 配置
CORS_ALLOW_ALL_ORIGINS = True

# 应用程序
INSTALLED_APPS = [
    # Django 默认应用
    'users',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # 第三方应用
    'colorfield',
    'admin_interface',
    'crispy_forms',

    # 其他自定义应用
    'recite',
    'rest_framework',
    'rest_framework_simplejwt',
    'corsheaders',
    'words'
]

# 中间件
MIDDLEWARE = [
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

# URL 配置
ROOT_URLCONF = "backend.urls"

# 模板设置
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],  # 确保包含自定义模板目录
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',  # 重要
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

# WSGI 应用
WSGI_APPLICATION = "backend.wsgi.application"

# 数据库配置
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',  # 使用 MySQL 后端
        'NAME': 'superde',                      # 数据库名称
        'USER': 'root',                         # 你的 MySQL 用户名
        'PASSWORD': 'xuanzhe0922.',             # 你的 MySQL 密码
        'HOST': 'localhost',                    # 数据库主机，通常为 localhost
        'PORT': '3306',                         # MySQL 默认端口
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
    }
}

# 密码验证
AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",},
    {"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",},
    {"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",},
]

# 国际化
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_TZ = True

# 静态文件设置
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static']
STATIC_ROOT = BASE_DIR / 'staticfiles'  # collectstatic 命令将静态文件收集到此目录

# Crispy Forms 配置

# 默认主键字段类型
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# 缓存配置
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'unique-snowflake',
    }
}

REST_FRAMEWORK = {
    # 其他设置
    'DEFAULT_THROTTLE_CLASSES': [
        'rest_framework.throttling.UserRateThrottle',
    ],
    'DEFAULT_THROTTLE_RATES': {
        'user': '300/min',
    },
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ),
}

from datetime import timedelta

SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(days=1),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=7),
    # 其他配置
}

AUTH_USER_MODEL = 'users.User'

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.qq.com'
EMAIL_PORT = 465
EMAIL_USE_TLS = False
EMAIL_USE_SSL = True
EMAIL_HOST_USER = '1298429535@qq.com'
EMAIL_HOST_PASSWORD = 'ymqehiignqlvjfhd'  # 确保这是QQ邮箱的授权码
DEFAULT_FROM_EMAIL = 'SuperDe <1298429535@qq.com>'

# 媒体文件配置
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
